package org.dallasrowing.logbook.ui.view;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public abstract class SwtBeanEditorWidgetBase<T> extends Composite implements BeanEditorWidget<T> {

	private Combo select;
	private Button cancelButton;
	private Button saveButton;
	private Button newButton;

	public SwtBeanEditorWidgetBase(Composite parent, int none) {
		super(parent, none);
		initWidgets();
		disableEditInternal();
	}

	protected abstract void createBeanFields();

	protected abstract void disableEdit();

	protected abstract void enableEdit();

	protected abstract String getBeanName(T bean);

	private void initWidgets() {
		setLayout(new GridLayout(2, false));
		
		select = new Combo(this, SWT.None | SWT.READ_ONLY);
		select.add("Some Name");
		select.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		newButton = new Button(this, SWT.PUSH);
		newButton.setText("Create New...");
		
		createBeanFields();
		
		saveButton = new Button(this, SWT.PUSH);
		saveButton.setText("Update");
		GridData gd = new GridData();
		gd.horizontalAlignment = GridData.END;
		saveButton.setLayoutData(gd);
		
		cancelButton = new Button(this, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.setFocus();
		gd = new GridData();
		gd.horizontalAlignment = GridData.BEGINNING;
		cancelButton.setLayoutData(gd);
	}

	public void fillBeans(List<T> beanList) {
		select.removeAll();
		for (T b : beanList)
		{
			select.add(getBeanName(b));
		}
	}

	public void onBeanSelected(final Runnable run) {
		select.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				run.run();
			}
		});
	}

	public int getSelectedBean() {
		return select.getSelectionIndex();
	}

	public void onCloseClicked(final Runnable with) {
		this.cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				with.run();
			}
		});
	}

	public void onCreateNewClicked(final Runnable with) {
		this.newButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				with.run();
			}
		});
	}			

	public void onUpdateClicked(final Runnable with) {
		this.saveButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event arg0) {
				with.run();
			}
		});
	}

	public void enableNew() {
		this.saveButton.setText("Create");
		select.deselectAll();
		enableEditInternal();
	}

	public void enableUpdate() {
		this.saveButton.setText("Update");
		enableEditInternal();
		layout(true, true);
		pack(true);
	}

	private void enableEditInternal() {
		this.saveButton.setEnabled(true);
		enableEdit();
	}

	private void disableEditInternal() {
		select.deselectAll();
		saveButton.setEnabled(false);
		disableEdit();
	}


	
}
